package com.youlai.boot.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.youlai.boot.common.annotation.Log;
import com.youlai.boot.common.enums.LogModuleEnum;
import com.youlai.boot.system.enums.RecycleStatusEnum;
import com.youlai.boot.system.mapper.RecycleRequestMapper;
import com.youlai.boot.system.model.entity.ProcessRecord;
import com.youlai.boot.system.model.entity.RecycleRequest;
import com.youlai.boot.system.model.query.RecycleRequestQuery;
import com.youlai.boot.system.model.vo.RecordItem;
import com.youlai.boot.system.model.vo.RecycleRequestVO;
import com.youlai.boot.system.service.ProcessRecordService;
import com.youlai.boot.system.service.RequestManagerService;
import com.youlai.boot.system.service.TransactionService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.youlai.boot.system.model.form.ProcessRecordForm;
import com.youlai.boot.system.model.query.ProcessRecordQuery;
import com.youlai.boot.system.model.vo.ProcessRecordVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.youlai.boot.common.result.PageResult;
import com.youlai.boot.common.result.Result;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 处理记录前端控制层
 *
 * @author youlaitech
 * @since 2025-02-28 16:53
 */
@RestController
@RequestMapping("/api/v1/records")
@RequiredArgsConstructor
public class ProcessRecordController {

    private final ProcessRecordService processRecordService;
    private final TransactionService transactionService;
    private final RecycleRequestMapper recycleRequestMapper;

    /**
     * 查询处理状态为0||-1（即主）
     * @param queryParams
     * @return
     */
    @Operation(summary = "处理记录分页列表")
    @GetMapping("/page")
    @Log(value = "处理记录分页列表", module = LogModuleEnum.RECYCLE)
    public PageResult<ProcessRecordVO> getProcessRecordPage(ProcessRecordQuery queryParams) {
        IPage<ProcessRecordVO> result = processRecordService.getProcessRecordPage(queryParams);
        return PageResult.success(result);
    }

    /**
     * 获取表单信息
     * @param id
     * @return
     */
    @GetMapping("/{id}/form")
    public Result<ProcessRecordVO> getRecordForm(@PathVariable("id") Long id) {
        ProcessRecordVO recordForm = processRecordService.getFormById(id);
        return Result.success(recordForm);
    }

    /**
     * 新增记录
     * @param processRecordForm
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> addRecord(@RequestBody @Valid ProcessRecordForm processRecordForm) {
        Boolean rs = processRecordService.addRecord(processRecordForm);
        return Result.success(rs);
    }

    /**
     * 更新记录
     * @param processRecordForm
     * @return
     */
    @PostMapping("/update")
    public Result<Boolean> updateRecord(@RequestBody @Valid ProcessRecordForm processRecordForm) {
        Boolean rs = processRecordService.updateRecord(processRecordForm);
        return Result.success(rs);
    }

    @DeleteMapping("/{ids}")
    public Result<Boolean> deleteRecords(@PathVariable("ids") String ids) {
        Boolean rs = processRecordService.removeRecords(ids);
        return Result.success(rs);
    }

    /**
     * 获取表单记录列表
     * @return
     */
    @GetMapping("/{requestId}/history")
    public Result<List<RecordItem>> getHistoryRecord(@PathVariable("requestId") Long requestId) {
        List<RecordItem> lists = processRecordService.getHistoryRecord(requestId);
        return Result.success(lists);
    }

    /**
     * 结束处理
     * @param requestId
     * @return
     */
    @GetMapping("/completed/{requestId}")
    public Result<Boolean> completedRecord(@PathVariable("requestId") Long requestId) {
        //1. 汇款给机构，由系统管理员进行模拟
        transactionService.transactionAmountToOrg(requestId);
        //2.修改主表状态
        ProcessRecord record = processRecordService.getOne(new LambdaQueryWrapper<ProcessRecord>()
                .eq(ProcessRecord::getRequestId, requestId).eq(ProcessRecord::getProcessStatus, 0));
        record.setProcessStatus(-1);
        processRecordService.updateById(record);
        RecycleRequest recycleRequest = recycleRequestMapper.selectById(requestId);
        recycleRequest.setStatus(RecycleStatusEnum.HANDLED.getCode());
        recycleRequest.setUpdateTime(LocalDateTime.now());
        recycleRequestMapper.updateById(recycleRequest);
        return Result.success();
    }

}
